# -*- coding: utf-8 -*-
# @Time    : 2023/10/10 21:31
# @Author  : yan.wei
# @Email   : 13675196684@163.com
# @File    : FileSplitViaData.py
# @Software: PyCharm


# @Author: wei.yan
# @Email:13675196684@163.com
# @Date: 2022-06-22 17:14:07
# @Last Modified by: wei.yan
# @Last Modified time: 2022-06-22 17:14:07

# -*- coding=utf-8 -*-


# """
# 单个csv文件拆分
# """

# import pandas as pd

# # 读取csv文件
# data = pd.read_csv(r'D:\Ipython\PythonDataMining\Case\VMAXDataMining\VoNR\DataPre\aggr_imscore_bearer_day.csv')

# # 获取clttime清单
# clttime_range = data['epsfb_procedurestarttime'].unique()
# clttime_range = clttime_range.tolist()

# # clttime_range
# # type(clttime_range)

# # 根据clttime进行文件拆分
# for value in clttime_range:
#     # print(value)
#     data[data['epsfb_procedurestarttime'] == value].to_csv('aggr_imscore_bearer_day_' + str(value) + '.csv',index = False, na_rep = 'N/A')


"""遍历文件夹，进行csv文件拆分
"""
import os
from os import path
import pandas as pd
import numpy as np
# import sqlite3


import os


def scaner_file(url):
# 遍历当前路径下所有文件
    file = os.listdir(url)
    savepath = r'E:\P04_Python_Case_IRAHO\DataPreparation\sqlite\oneday2seven\result\\'   # 设置保存路径，注意结尾双斜杠
    # savepath = r'D:\Ipython\PythonDataMining\Case\VMAXDataMining\VoNR\DataSet\\'  # 设置保存路径，注意结尾双斜杠
    for f in file:
        if get_suffix(f) != 'csv':
            continue
        # 字符串拼接
        real_url = path.join(url, f)
        table_name = getTableName(f)
        print('table_name ' + table_name)

        data = pd.read_csv(real_url)

# 将2000-1-1格式改为20000101
        # data['epsfb_procedurestarttime'] = pd.to_datetime(data['epsfb_procedurestarttime'])
        #
        # data['epsfb_procedurestarttime'] = data['epsfb_procedurestarttime'].apply(lambda x: x.strftime('%Y%m%d'))
        #
        # clttime_range = data['epsfb_procedurestarttime'].uniqQue()
        # clttime_range = clttime_range.tolist()

        datacolumn0 = data['epsfb_procedurestarttime']

        datacolumn0 = pd.to_datetime(datacolumn0)

        datacolumn0 = datacolumn0.apply(lambda x: x.strftime('%Y%m%d'))

        clttime_range = datacolumn0.unique()
        clttime_range = clttime_range.tolist()

        """        
        clttime_range = data['epsfb_procedurestarttime'].unique()
        # clttime_range = clttime_range.apply(lambda x:x.strftime('%Y-%m-%d %H:%M:%S'))  #时间转字符串
        clttime_range = clttime_range.tolist()
        """

        for value in clttime_range:
            # print(value)
            # data[data['epsfb_procedurestarttime'] == value].to_csv(table_name + '_' + str(value) + '.csv',index = False, na_rep = 'N/A')
            data[pd.to_datetime(data['epsfb_procedurestarttime']).apply(lambda x: x.strftime('%Y%m%d')) == value].to_csv(
                savepath + table_name + '_' + str(value) + '.csv', index=False,
                na_rep='N/A')

        # intoDB(real_url, table_name,conn)
        # 打印出来
        print(real_url)


def getTableName(file_url):
    _file_name = file_url  # 文件名称不带后缀
    if '.' in file_url:
        _file_name = file_url.split('.')[0]
    return _file_name


def get_suffix(file_url):
    _suffix = ''  # 后缀
    if '.' in file_url:
        _suffix = file_url.split('.')[-1]
    return _suffix


# scaner_file(r'D:\Ipython\PythonDataMining\Case\VMAXDataMining\Downloads111')
scaner_file(r'E:\P04_Python_Case_IRAHO\DataPreparation\sqlite\oneday2seven\tmp')